// pages/me/me.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag:0,
   userInfo:{},
   hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    checked: true,//全选状态
    count:'',//加入购物车数量
    price:0,//总价钱
    goodsList:[],
    goodsList:[
      { 
        title:'Black Fungus',
        prOne:5,
        prTwo:7,
        checkedOne: true,
        img: '/people/cai1.jpg',
        intro: 'Wild edible fungus 500g  >',
        num: 0
      },
      { 
        title:'Maoshao Facial',
        prOne:10,
        prTwo:12,
        checkedOne: true,
        img: '/people/cai2.png',
        intro: 'green 50cm        >',
        num: 0
      },
    ],
  },
  // 全选
  checkBtn: function (e) {
    var that = this
    // 全选ICON默认选中
    let checked = this.data.checked;
    // true  -----   false
    checked = !checked;
    // 获取商品数据
    let goodsList = this.data.goodsList;
    // 循环遍历判断列表中的数据是否选中
    for (let i = 0; i < goodsList.length; i++) {
      goodsList[i].checkedOne = checked;
    }
    // 页面重新渲染
    this.setData({
      checked: checked,
      goodsList: goodsList
    });
    this.zPrice()
  },
  // 单选
  checkBtnOne:function(e){
    var that = this;
    // 获取选中的单选框索引
    var index = e.currentTarget.dataset.index;
    // 获取到商品列表数据
    var goodsList = that.data.goodsList;
    // 默认全选
    that.data.checked = true;
    // 循环数组数据，判断----选中/未选中[selected]
    goodsList[index].checkedOne = !goodsList[index].checkedOne;
    // 如果数组数据全部为selected[true],全选
    for (var i = goodsList.length - 1; i >= 0; i--) {
      if (!goodsList[i].checkedOne) {
        that.data.checked = false;
        break;
      }
    }
    // 重新渲染数据
    that.setData({
      goodsList: goodsList,
      checked: that.data.checked
    })
    this.zPrice()
  },
  // 加数量
  jiaBtn: function (e){
    var that=this
    let index = e.currentTarget.dataset.index
    let goodsList=that.data.goodsList
    let num = that.data.goodsList[index].num
    num=num+1
    goodsList[index].num=num
    console.log(that.data.goodsList[index].num)
    that.setData({
      goodsList: goodsList,
      addGoods: true,
      addGooding: false,
    })
    this.zPrice()
  },
  //减数量
  jianBtn: function (e) {
    var that = this
    let index=e.currentTarget.dataset.index
    let goodsList=that.data.goodsList
    let num =goodsList[index].num
    if (num<=0){
      return false;
    }
    num = num - 1
    goodsList[index].num = num
    that.setData({
      goodsList: goodsList,
      count: that.data.goodsList[index].num,
    })
    this.zPrice()
  },
  // 计算总价
  zPrice(){
    var that = this
    let goodsList=that.data.goodsList;
    var prc = 0;
    for (let i = 0; i < goodsList.length; i++) {
      if (goodsList[i].checkedOne){
        prc += goodsList[i].num * goodsList[i].prOne;
      }
    }
    that.setData({
      goodsList: goodsList,
      price: prc,
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  switchnav:function(e){
    console.log(e)
    var id=e.currentTarget.id
    if(this.data.flag==id)
    return false;
    else
    this.setData({flag:id})
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // if(app.globalData.userInfo){
    //  var userInfo=app.globalData.userInfo
    // this.setData({
    //     userInfo: userInfo
    // })
    // }
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})